<?php

namespace eqphp;
class logger{

    static function __callStatic($name, $param){
        //读取配置文件
        $name = strtolower($name);
        $log = (object)system::config('logger');

        if ($log->switch['run'] && $log->store_level && in_array($name, $log->level)) {
            if (is_array($log->store_level) && !in_array($name, $log->store_level)) {
                return false;
            }

            //写日志
            $run_dir = PATH_LOG . 'run/';
            list($date, $time) = explode('@', date('ymd@H:i:s'));
            $data = '[' . $time . '] ' . $name . ': ' . (string)$param[0] . PHP_EOL;
            $file_name = ($log->is_mix ? 'mix' : $name) . '_' . $date . '.log';
            if (defined('MODULE_NAME') && $log->is_module_save) {
                is_dir($run_dir . MODULE_NAME) or mkdir($run_dir . MODULE_NAME, 0777);
                $file_name = MODULE_NAME . '/' . $file_name;
            }
            self::record_log($run_dir . $file_name, $data);

            //TODO 报警（send alarm mail or message）
            if ($log->alarm['mode'] && in_array($name, array('warn', 'error'))) {
                if (in_array($log->alarm['mode'], array('both', 'email'))) {
                }
                if (in_array($log->alarm['mode'], array('both', 'message'))) {
                }
            }
        }
    }

    static function exception($type, $data){
        if (config('logger.switch.exception')) {
            $file_name = LOG_TOPIC . $type . '.log';
            $data = '[' . date('y-m-d H:i:s') . '] ' . $data . PHP_EOL;
            self::record_log($file_name, $data);
        }
    }

    //type:mysql,mssql,pgsql,sqlite,access,mongo,redis
    static function db($data, $type = 'mysql', $is_read = true){
        if (config('logger.switch.db')) {
            $suffix = $is_read ? '_r.log' : '_w.log';
            list($date, $time) = explode('@', date('ymd@H:i:s'));
            $file_name = PATH_LOG . $type . '/' . $date . $suffix;
            $data = '[' . $time . '] ' . $data . PHP_EOL;
            self::record_log($file_name, $data);
        }
    }

    static function visit($ip = ''){
        if (config('logger.switch.visit')) {
            if (empty($ip) && isset($_SERVER['REMOTE_ADDR'])) {
                $ip = $_SERVER['REMOTE_ADDR'];
            }
            $data = $ip . ' [' . date('H:i:s') . ']';
            if (isset($_SERVER['REQUEST_METHOD'])) {
                $data .= ' ' . strtoupper($_SERVER['REQUEST_METHOD']);
            }
            if (isset($_SERVER['REQUEST_URI'])) {
                $data .= ' "' . substr($_SERVER['REQUEST_URI'], 0, 255) . '"';
            }
            if (isset($_SERVER['HTTP_USER_AGENT'])) {
                $data .= ' "' . $_SERVER['HTTP_USER_AGENT'] . '"';
            }
            $file_name = PATH_LOG . 'visit/' . date('ymd') . '.log';
            self::record_log($file_name, $data . PHP_EOL);
        }
    }

    static function record_log($file_name, $data){
        if (config('logger.is_write_now')) {
            file_put_contents($file_name, $data, FILE_APPEND);
        } else {
            $GLOBALS['_LOG'][$file_name][] = $data;
        }
    }

}